package com.schematic.flixel.examples
{
import org.flixel.FlxG;
import org.flixel.FlxState;
import org.flixel.FlxTilemap;
import org.flixel.FlxU;


public class TilemapState extends ExampleState
{

    //--------------------------------------------------------------------------
    //
    //  Constants
    //
    //--------------------------------------------------------------------------
	
	//----------------------------------
	//  ARM_KEY
	//----------------------------------
	/**
	 * Sets the key for the hero to arm itself
	 */
	public static const ARM_KEY:String = "ONE";

    //----------------------------------
    //  MAP_DATA
    //----------------------------------
    [Embed(source="/../assets/data/desert.txt", mimeType="application/octet-stream")]
    /**
     * The CSV file with the tile map data
     */
    public static const MAP_DATA:Class;
	//----------------------------------
	//  MUSIC
	//----------------------------------	
	[Embed(source="../../asset/Mario_Bros.mp3")]
	private var SoundLoop:Class;
	

    //--------------------------------------------------------------------------
    //
    // Constructor
    //
    //--------------------------------------------------------------------------

    /**
     * Creates a new instance of this class.
     */
    public function TilemapState()
    {
        super();
    }
    
    //--------------------------------------------------------------------------
    //
    //  Variables
    //
    //--------------------------------------------------------------------------

    //----------------------------------
    //  mario
    //----------------------------------
    /**
     *  @private
     */
    private var mario:Mario;
	
    //----------------------------------
    //  tileMap
    //----------------------------------
    /**
     *  @private
     */
    private var tileMap:FlxTilemap;
	
	private var isArmed:Boolean;
	
	private var isCrashing:Boolean;
    
    //--------------------------------------------------------------------------
    //
    //  Overridden Methods - FlxState
    //
    //--------------------------------------------------------------------------

    override public function create():void
    {
        super.create();

        tileMap = new FlxTilemap();
        tileMap.drawIndex = 0;
        tileMap.collideIndex = 27;
        tileMap.loadMap(new MAP_DATA(), Skins.MAP_TILES, 32, 32);
        tileMap.render();
        tileMap.y = 30;
        add(tileMap);
        
        mario = new Mario();
        mario.y = 224;
        add(mario);
		FlxG.playMusic(SoundLoop,0.2);
		isCrashing = false;
    }
    
    override public function update():void
	{
		if (FlxG.keys.justPressed("X")) 
			isArmed = !isArmed;
		
        if (!FlxU.collide(tileMap, mario))
        {
			if (FlxG.keys.UP)
			{
				mario.walkUp();
			}
			else if (FlxG.keys.DOWN)
			{
				mario.walkDown();
			}
			else if (FlxG.keys.LEFT && FlxG.keys.SPACE)
			{
				mario.weaponHitLeft();				
			}
			else if (FlxG.keys.RIGHT && FlxG.keys.SPACE)
			{
				mario.weaponHitRight();
			}
			else if (FlxG.keys.RIGHT)
            {
				if (isArmed)
					mario.weaponWalkRight();
				else
					mario.walkRight();
            }
            else if (FlxG.keys.LEFT)
            {
				if (isArmed)
					mario.weaponWalkLeft();
				else
                mario.walkLeft();
            }
		    else
    	    {
				//mario.marioDieLeft();
				//mario.stand();
				if (isCrashing)
					mario.marioDieLeft();
				else mario.stand();
				trace("perro");
      		}  
		}
		else
		{
			mario.marioDieLeft();
			//mario.stand();
			trace("gato");
		}  
  	
		        
        super.update();
  	  }
	}
}
